﻿package little.bunny.adapter;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import little.bunny.R;

public class List_Adapter extends BaseAdapter {
	//private Context context;
	private ArrayList<String[]> Str1,LeftStr,RightStr;
	private LayoutInflater myInflater;
	private int Num1,Num2,XmlNum;
	
	public List_Adapter() {
		super();
	}

	/**
	 * 
	 * @param context 
	 * @param str1  配备的数据（二维数组）
	 * @param num1 listView中现实用的文字  所在的数组 位置
	 * @param num2  同上
	 * @param xmlNum 为listview的items选择 使用的xml布局文件
	 */
	public List_Adapter(Context context, ArrayList<String[]> str1 ,int num1,int num2,int xmlNum) {
		super();
		////this.context = context;
		this.Str1 = str1;
		this.Num1=num1;
		this.Num2=num2;
		this.XmlNum=xmlNum;
		myInflater=LayoutInflater.from(context);
		//System.out.println("List_Adapter中输出二维数组的大小："+str1.length);
		
		if(xmlNum==R.layout.values_lists_items){
			LeftStr=new ArrayList<String[]>();
					//new String[str1.length/2][str1[0].length];
			RightStr=new ArrayList<String[]>();;
					//new String[str1.length/2][str1[0].length];
			int num=0;
			if(str1.size()%2==0){
				num=str1.size();
			}else{
				num=str1.size()-1;
			}
			for(int i=0;i<num;i+=2){
				///for(int j=0;j<str1.get(i).length;j++){
					 //int a=i/2;
					//LeftStr[a][j]=str1[i][j];
					 LeftStr.add(str1.get(i));
				//}
			}
			for(int i=1;i<num;i+=2){
				///for(int j=0;j<str1[i].length;j++){
					///int a=i/2;
					////RightStr[a][j]=str1[i][j];
				RightStr.add(str1.get(i));
				//}
			}
		}
		
	}

	public int getCount() {
		// TODO Auto-generated method stub
		if(XmlNum==R.layout.values_lists_items){
			return RightStr.size();
		}else{
			return Str1.size();
		}
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		if(XmlNum==R.layout.values_lists_items){
			return RightStr.get(arg0);
		}else{
			return Str1.get(arg0);
		}
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		ViewHolder holder = null;
		if (convertView == null) {
			// 自定义list_items为layout
			convertView = myInflater.inflate(XmlNum, null);
			/* 初始化holder */
			holder = new ViewHolder();
			 if(XmlNum==R.layout.nei_list_items){
				 holder.n_title = (TextView) convertView.findViewById(R.id.news_title);
				 holder.n_time = (TextView) convertView.findViewById(R.id.news_time);
			 }
			 if(XmlNum==R.layout.list_lists_items){
				 holder.shop_name=(TextView)convertView.findViewById(R.id.shops_title);
			 }
			 if(XmlNum==R.layout.values_lists_items){
				 holder.price_t1=(TextView)convertView.findViewById(R.id.price_title1);
				 holder.price_v1=(TextView)convertView.findViewById(R.id.price_value1);
				 holder.price_t2=(TextView)convertView.findViewById(R.id.price_title2);
				 holder.price_v2=(TextView)convertView.findViewById(R.id.price_value2);
			 }
			//holder.n_title = (TextView) convertView.findViewById(R.id.news_title);
			//holder.n_time = (TextView) convertView.findViewById(R.id.news_time);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// File f = new File(paths.get(position).toString());
		 if(XmlNum==R.layout.nei_list_items){
			 holder.n_title.setText(Str1.get(position)[Num1].toString());
			 //holder.n_title.setBackgroundColor(Color.argb(255, 0, 255, 0))
			 holder.n_time.setText(Str1.get(position)[Num2].toString());
		}
		if(XmlNum==R.layout.list_lists_items){
			holder.shop_name.setText(Str1.get(position)[Num1].toString());
		}
		if(XmlNum==R.layout.values_lists_items){
			 holder.price_t1.setText(LeftStr.get(position)[Num1].toString().replaceAll("<.*?>|\\s*|.*?>", ""));
			 holder.price_v1.setText(LeftStr.get(position)[Num2].toString().replaceAll("<.*?>|\\s*|.*?>", ""));
			 holder.price_t2.setText(RightStr.get(position)[Num1].toString().replaceAll("<.*?>|\\s*|.*?>", ""));
			 holder.price_v2.setText(RightStr.get(position)[Num2].toString().replaceAll("<.*?>|\\s*|.*?>", ""));
		 }
		return convertView;
		
	}
	
	/**
	 * 不单独写get set可以提高效率 class ViewHolder
	 * */
	private class ViewHolder {
		TextView n_title;
		TextView n_time;
		TextView shop_name;
		TextView price_t1;
		TextView price_v1;
		TextView price_t2;
		TextView price_v2;
		
	}

}
